/**
 * 
 */
package projeto.administrativo.servico.cgd;

import java.util.Collection;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import plataforma.base.cdp.Criterio;
import plataforma.base.cdp.ObjetoPagina;
import plataforma.crudestatico.cgd.DaoBase;
import projeto.administrativo.servico.cdp.KServico;
import projeto.administrativo.servico.cdp.OrdemKServicoKAtividade;

/**
 * @author mike
 *
 */
@Repository
public class DaoKServico extends DaoBase<KServico> {
	
	@Override
	public KServico recuperarPorId(Long id) {
		String hql = "select distinct ks from KServico ks "				
				+ " left outer join fetch ks.ordens "
				+ " left outer join fetch ks.categoria "
				+ " where ks.id=:parId";

		Query query = getEntityManager().createQuery(hql);
		query.setParameter("parId", id);

		return (KServico) query.getSingleResult();
	}
	
	//TODO NANDOPHO Passar para tipo parametrizado
			public Collection<KServico> recuperarTodosPaginado(ObjetoPagina objetoPagina) {


				StringBuilder sb = new StringBuilder();
				sb.append("select k from KServico k left outer join fetch k.categoria ");

				// adicionando where
				sb.append(" where 1=1 ");

				if (objetoPagina.getCriteriosSimples() != null) {

					for (Criterio criterio : objetoPagina.getCriteriosSimples()) {

						sb.append(" and " + criterio.toHQL());

					}

				}
				if (objetoPagina.getCriterioOrdenacao() != null)
					sb.append(" order by "
							+ objetoPagina.getCriterioOrdenacao().toString());

				Query consulta = getEntityManager().createQuery(sb.toString());
				consulta.setFirstResult(objetoPagina.getFirstResults());
				consulta.setMaxResults(objetoPagina.getMaxResults());

				return (Collection<KServico>) consulta.getResultList();

			}
			
			
			@Override
			public int recuperarQteTodos(ObjetoPagina objetoPagina) {
						
				StringBuilder sb = new StringBuilder();
				sb.append(" select count(id) from KServico k");
				// adicionando where
				sb.append(" where 1=1 ");

				if (objetoPagina.getCriteriosSimples() != null) {

					for (Criterio criterio : objetoPagina.getCriteriosSimples()) {

						sb.append(" and " + criterio.toHQL());

					}

				}

				Long count = (Long) getEntityManager().createQuery(sb.toString())
						.getSingleResult();

				return count.intValue();
			}

			public KServico recuperarPorIdParaCopiar(Long id) {
				String hql = "select distinct k from KServico k "				
						+ " inner join fetch k.ordens ord"
						+"  left outer join fetch ord.kAtividade at "
						+"  left outer join fetch at.listaKChecklist check "
						+"  left outer join fetch check.listaKItemChecklist "
						+"  where k.id=:parId";

				Query query = getEntityManager().createQuery(hql);
				query.setParameter("parId", id);

				return (KServico) query.getSingleResult();
			}
			
			
			public List<OrdemKServicoKAtividade> recuperarPorKServicoParaCopiar(Long id) {
				String hql = "select distinct ord from OrdemKServicoKAtividade ord "				
						+"  left outer join fetch ord.kAtividade at "
						+"  left outer join fetch at.listaKChecklist check "
						+"  left outer join fetch check.listaKItemChecklist "
						+"  where ord.kServico.id=:parId";

				Query query = getEntityManager().createQuery(hql);
				query.setParameter("parId", id);

				return query.getResultList();
			}



}
